package com.hello.sky.util;

import io.jsonwebtoken.*;
import jakarta.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.HashMap;

public class JwtUtil {

    public static final String SECRET_KET = "MySkyTakeOut";

    public static String getJwt(HashMap map){

        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, SECRET_KET)
                .addClaims(map)
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 3600 * 24))
                .compact();
    }

    public static boolean parseJwt(String jwt){
        try {
            Claims body = Jwts.parser()
                    .setSigningKey(SECRET_KET)
                    .parseClaimsJws(jwt)
                    .getBody();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public static Claims parseJwtThenGet(String jwt) {
        Claims body;
        try {
            body = Jwts.parser()
                    .setSigningKey(SECRET_KET)
                    .parseClaimsJws(jwt)
                    .getBody();
        } catch (Exception e) {
            return null;
        }
        return body;
    }

    public static Claims parseJwtThenReturnId(String jwt) {
        Claims body;
        try {
            body = Jwts.parser()
                    .setSigningKey(SECRET_KET)
                    .parseClaimsJws(jwt)
                    .getBody();
        } catch (Exception e) {
            return null;
        }
        return body;
    }

}
